package TrafficSim;

import java.util.Vector;

import javax.vecmath.Vector2d;

public class TrafficLightController {

	Vector<TrafficLight> TrafficLightVector;
	int state;
	private Vector<Street> incomingStreets;
	private boolean first = true;
	private double oldTime;
	// 0 : off, 1 : blink, 2 : north-south =red, 3 : active

	private double time;
	private double sT;

	TrafficLightController(Intersection i) {
		this.TrafficLightVector = new Vector<TrafficLight>();
		this.incomingStreets = i.getIncomingStreets();
		setTrafficLightFlags(i);
		
		state = 0;
	}
	
	public Vector<TrafficLight> getTrafficLightVector() {
		return TrafficLightVector;
	}
	
	private void setTrafficLightFlags(Intersection i){
		Vector2d vec1 = null;
		Vector2d vec2 = null;
		vec1 = new Vector2d(i.getIncomingStreets().get(0).getDirection());
		TrafficLight tl = new TrafficLight(1);
		i.getIncomingStreets().get(0).placeTrafficLight(tl);
		TrafficLightVector.add(tl);
		for (int j = 1; j < i.getIncomingStreets().size(); j++) {
			vec2 = new Vector2d(i.getIncomingStreets().get(j).getDirection());
			if (vec1.angle(vec2) > 2.2) {
				tl = new TrafficLight(1);
				incomingStreets.get(j).placeTrafficLight(tl);
				TrafficLightVector.add(tl);
			} else {
				tl = new TrafficLight(0);
				incomingStreets.get(j).placeTrafficLight(tl);
				TrafficLightVector.add(tl);
			}
		}
	}

	public Vector<Street> getIncomingStreets() {
		return this.incomingStreets;
	}

	public void turnOn() {
		if (state == 0) {
			for (TrafficLight tl : TrafficLightVector) {
				tl.setBlink();
			}
			state = 1;
		}
	}

	public void toggleOn() {
		if (state == 1) {
			for (TrafficLight tl : TrafficLightVector) {
				if (tl.getId() == 1) {
					tl.setRed();
				} else {
					tl.setGreen();
				}
			}
			state = 2;
		}
	}

	public void switchState(double sT, double oldTime) { // sT => switchTime
		if (first) {
			this.sT = sT;
			this.oldTime = oldTime;
			first = false;
		}
		time = System.currentTimeMillis();
		TrafficLight.State tls = this.TrafficLightVector.get(0).getTlState();

		if (tls == TrafficLight.State.YELLOW
				|| tls == TrafficLight.State.REDYELLOW) {
			if ((time - this.oldTime) >= 3000) {
				if (state == 2 || state == 3) {
					for (TrafficLight tl : TrafficLightVector) {
						tl.switchState();
					}
					state = 3;
					first = true;
				}
			}
			return;
		}

		if ((this.sT - time) < 0) {
			if (state == 2 || state == 3) {
				for (TrafficLight tl : TrafficLightVector) {
					tl.switchState();
				}
				state = 3;
				first = true;
			}
		}
	}

	public void toggleOff() {
		if (state == 2 || state == 3) {
			for (TrafficLight tl : TrafficLightVector) {
				tl.setBlink();
			}
			state = 1;
		}
	}

	public void turnOff() {
		if (state == 1) {
			for (TrafficLight tl : TrafficLightVector) {
				tl.turnOff();
			}
			state = 0;
		}
	}

	public TrafficLight.State getTrafficLightState() {
		return this.TrafficLightVector.get(0).getTlState();
	}

	public void switchStateStupid() {
		if (state == 2 || state == 3) {
			for (TrafficLight tl : TrafficLightVector) {
				tl.switchState();
			}
			state = 3;
		}
	}
	
	
	public void newSwitchState() { // sT => switchTime
		for (TrafficLight tl : TrafficLightVector) {
			tl.switchState();
		}
	}
}